package vip.xiaonuo.biz.modular.lancerCategory.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.pojo.domain.BizCategory;
import vip.xiaonuo.biz.modular.pojo.dto.add.CategoryAddDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDeleteDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDetailDTO;
import vip.xiaonuo.biz.modular.pojo.dto.edit.CategoryEditDTO;
import vip.xiaonuo.biz.modular.pojo.dto.page.CategoryPageDTO;
import vip.xiaonuo.biz.modular.lancerCategory.service.BizCategoryService;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 管理员分类控制器
 *
 * @author admin
 * @date 2024/01/02
 */
@Api(tags = "分类管理(管理端)")
@ApiSupport(author = "LANCER_TEAM", order = 1)
@Validated
@RestController
@RequestMapping
public class AdminCategoryController {

    @Autowired
    private BizCategoryService CategoryService;
    @Autowired
    BizCategoryService categoryService;

    @ApiOperationSupport(order = 6)
    @ApiOperation("管理端获取分类树选择器")
    @GetMapping("/biz/admin/category/categoryTreeSelector")
    @SaCheckPermission("/biz/admin/category/categoryTreeSelector")

    public CommonResult<List<Tree<String>>> categoryTreeSelector() {
        return CommonResult.data(categoryService.tree());
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("管理端获取分类树")
    @GetMapping("/biz/admin/category/tree")
    @SaCheckPermission("/biz/admin/category/tree")

    public CommonResult<List<Tree<String>>> tree() {
        return CommonResult.data(categoryService.tree());
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation("管理端分类分页")
    @GetMapping("/biz/admin/category/page")
    @SaCheckPermission("/biz/admin/category/page")
    public CommonResult<Page<BizCategory>> page(@Valid CategoryPageDTO dto) {
        Page<BizCategory> res = CategoryService.pageLAdmin(dto);
        return CommonResult.data(res);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("管理端分类详情查询")
    @GetMapping("/biz/admin/category/detail")
    @SaCheckPermission("/biz/admin/category/detail")
    public CommonResult<BizCategory> detail(@Valid CommonDetailDTO dto) {
        BizCategory res = CategoryService.detail(dto);
        return CommonResult.data(res);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("管理端分类新增")
    @PostMapping("/biz/admin/category/add")
    @SaCheckPermission("/biz/admin/category/add")
    public CommonResult<String> add(@Valid @RequestBody CategoryAddDTO dto) {
        CategoryService.add(dto);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("管理端分类删除")
    @PostMapping("/biz/admin/category/delete")
    @SaCheckPermission("/biz/admin/category/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "删除id不能为空") List<CommonDeleteDTO> dto) {
        CategoryService.delete(dto);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("管理端分类编辑")
    @PostMapping("/biz/admin/category/edit")
    @SaCheckPermission("/biz/admin/category/edit")
    public CommonResult<String> edit(@RequestBody @Valid CategoryEditDTO dto) {
        CategoryService.edit(dto);
        return CommonResult.ok();
    }

}